package com.quo.service;

import com.order.hit.crossover.platform.common.bean.order.Order;
import com.order.hit.crossover.platform.common.bean.order.OrderState;
import com.order.hit.crossover.platform.common.bean.order.OrderType;
import com.order.hit.crossover.platform.common.bean.order.Quotation;
import com.order.hit.crossover.platform.common.dao.OrderDao;
import com.order.hit.crossover.platform.common.dao.QuotationDao;
import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.order.hit.crossover.platform.common.utils.ValueStrategy;
import com.quo.mapper.QuotationMapper;
import com.quo.utils.OverTimeJudge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Component
@Qualifier("QuotationServiceImpl")
public class QuotationServiceImpl implements QuotationService
{
    @Autowired
    private QuotationMapper quotationMapper;

    @Autowired
    private OverTimeJudge overTimeJudge;


    @Override
    public List<Quotation> selectAll()
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectAll()) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public List<Quotation> selectByOrderId(String orderId)
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectByOrderId(orderId)) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public List<Quotation> selectForBuy()
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectByType(OrderType.QUOTATION_BUY.toString())) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public List<Quotation> selectForSold()
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectByType(OrderType.QUOTATION_SOLD.toString())) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public List<Quotation> selectBuyOfStore(String storeId)
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectByTypeOfStore(OrderType.QUOTATION_BUY.toString(), storeId)) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public List<Quotation> selectSoldOfStore(String storeId)
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        List<Quotation> quotationList = new ArrayList<>();

        for(QuotationDao e:quotationMapper.selectByTypeOfStore(OrderType.QUOTATION_SOLD.toString(), storeId)) {
            quotationList.add(QuotationDao.getQuotation(e));
        }

        return quotationList;
    }

    @Override
    public Quotation selectById(String quotationId)
    {
        this.overTimeJudge.judgeQuotationIfOverTime();
        return QuotationDao.getQuotation(quotationMapper.selectById(quotationId));
    }

    @Override
    public void add(QuotationDao quotationDao)
    {
        quotationMapper.add(quotationDao);
    }

    @Override
    public void update(QuotationDao quotationDao)
    {
        quotationMapper.update(quotationDao);
    }

    @Override
    public void deletebyId(String quotationId)
    {
        quotationMapper.deletebyId(quotationId);
    }

    @Override
    public void deletebyOrderId(String orderId)
    {
        quotationMapper.deletebyOrderId(orderId);
    }

    @Override
    public List<Quotation> selectByOrderIdAndstrategy(String id, int strategy) {
        this.overTimeJudge.judgeQuotationIfOverTime();
        //获取该订单的所有报价单
        List<Quotation> list = this.selectByOrderId(id);
        OrderDao orderDao = OrderDao.getOrderDao((Order)new RestTemplate().exchange(
                "http://order:18081/orderService/getOrderById/"+id,
                HttpMethod.GET,
                new HttpEntity<>(new HttpHeaders()),
                PFResponse.class).getBody().get("data"));



        if(strategy == 0){
            // 按照价值排序
            if(orderDao.getOrderType().equalsIgnoreCase(OrderType.ORDER_BUY.toString())){
                // 说明是求购订单，价格从低到高
                return ValueStrategy.lowPrice(list);
            }else{
                // 说明是求售订单， 价格从高到低
                return ValueStrategy.highPrice(list);
            }
        }else if(strategy == 1){
            // 按照距离排序
            String storeLocation = orderDao.getStoreLocation();
            return ValueStrategy.shortDistance(list, storeLocation);
        } else if (strategy == 2){
            return ValueStrategy.longLife(list);
        } else if (strategy == 3){
            return ValueStrategy.sameBrand(OrderDao.getOrder(orderDao).getGood(),list);
        } else if (strategy == 4){
            List<Quotation> result = new ArrayList<>();
            // 按照价值排序
            if(orderDao.getOrderType().equalsIgnoreCase(OrderType.ORDER_BUY.toString())){
                // 说明是求购订单，价格从低到高
                List<Quotation> list1 =  ValueStrategy.lowPrice(list);
                for (Quotation q: list1){
                    if (q.getPrice() <= orderDao.getPrice()){
                        result.add(q);
                    }else{
                        break;
                    }
                }
            }else{
                // 说明是求售订单， 价格从高到低
                List<Quotation> list1 =  ValueStrategy.highPrice(list);
                for (Quotation q: list1){
                    if (q.getPrice() >= orderDao.getPrice()){
                        result.add(q);
                    }else{
                        break;
                    }
                }
            }
            return result;
        }
        return null;
    }

    @Override
    public Quotation getSuccessQuo(String orderId) {
        List<Quotation> list = this.selectByOrderId(orderId);
        OrderDao orderDao = OrderDao.getOrderDao((Order)new RestTemplate().exchange(
                "http://order:18081/orderService/getOrderById/"+orderId,
                HttpMethod.GET,
                new HttpEntity<>(new HttpHeaders()),
                PFResponse.class).getBody().get("data"));
        if (orderDao == null || !orderDao.getOrderState().equals(OrderState.getNameByCode(1).toString())){
            return null;
        }
        for (Quotation quotation:list){
            if (quotation.getQuotationState().equals(OrderState.QUOTATION_ACCEPT)){
                return quotation;
            }
        }
        return null;
    }

    @Override
    public Order getOrderByquoId(String quoId) {
        return (Order)new RestTemplate().exchange(
                "http://order:18081/orderService/getOrderById/"+this.quotationMapper.selectById(quoId).getOrderId(),
                HttpMethod.GET,
                new HttpEntity<>(new HttpHeaders()),
                PFResponse.class).getBody().get("data");
    }
}
